package com.freesun.shop.config;



import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
import java.util.UUID;

/**
 * 修改系统使用我们自己定义的头里面的Token,而不是使用JessionID
 */
public class TokenSessionManager extends DefaultWebSessionManager {

    private static final String TOKEN_HEARD_NAME="Authorization";
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        //从前端的头里面
        String token = WebUtils.toHttp(request).getHeader(TOKEN_HEARD_NAME);
        if(!StringUtils.hasText(token)){
            token= UUID.randomUUID().toString();
        }
        return token;
    }
}
